﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace ProvinceCityLinkage
{
    [WebService]
    public partial class ajaxWab : System.Web.UI.Page
    {
        ProvinceCityDataContext DB = new ProvinceCityDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获得所有省份列表
        /// </summary>
        /// <returns>省份列表,json 字符串</returns>
        [WebMethod]
        public String GetProvinces()
        {
            var provinces = DB.T_Province_City.Where(w => w.ID == w.ParentID).Select(s => new { ID = s.ID, Province_City_Name = s.Province_City_Name });
            Dictionary<Int32, String> dictProvince = provinces.ToDictionary(p => p.ID, p => p.Province_City_Name);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(dictProvince);
        }

        /// <summary>
        /// 获得指定省份下的所有城市列表
        /// </summary>
        /// <param name="provinceID">指定省份的ID</param>
        /// <returns>城市列表,json字符串</returns>
        [WebMethod]
        public String GetCitysByID(Int32 provinceID)
        {
            var citys = DB.T_Province_City.Where(w => w.ParentID == provinceID).Select(s => new { ID = s.ID, Province_City_Name = s.Province_City_Name });
            Dictionary<Int32, String> dictCity = citys.ToDictionary(p => p.ID, p => p.Province_City_Name);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(dictCity);
        }

    }
}